package com.combetter.changxing.ui.dialog;

import android.app.Activity;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import com.jakewharton.rxbinding2.view.RxView;
import com.combetter.changxing.R;
import com.combetter.changxing.databinding.DialogFacilityRechristenBinding;
import com.combetter.changxing.util.ResUtils;

import static com.combetter.changxing.ui.fragment.FacilityFragmentReWrite.RECHRISTEN_CODDE;

/**
 * 设备重命名dialog
 * <p>
 * Created by 深圳唐龙飞讯科技有限公司 on 2018/7/18.
 */
public class FacilityRechristenDialog extends DialogFragment {
    private DialogFacilityRechristenBinding mBinding;
    private String facilityName;
    private String identifierId;
    private int mid;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        mBinding = DataBindingUtil.inflate(inflater, R.layout.dialog_facility_rechristen, container, false);
        initVies();
        setListener();
        return mBinding.getRoot();
    }

    @Override
    public void onResume() {
        super.onResume();
        int width = ResUtils.getDimens(getContext(), R.dimen.DIMEN_450PX);
        int height = ResUtils.getDimens(getContext(), R.dimen.DIMEN_250PX);
        getDialog().getWindow().setLayout(width, height);
    }


    /**
     * 初始化控件
     */
    private void initVies() {
        facilityName = getArguments().getString("facilityName");
        identifierId = getArguments().getString("identifierId");
        mid = getArguments().getInt("mid");
        mBinding.etName.setText(facilityName);
        mBinding.etName.setSelection(mBinding.etName.length());
    }

    /**
     * 点击事件
     */
    private void setListener() {
        RxView.clicks(mBinding.tvCancel).subscribe(o -> dismiss());
        RxView.clicks(mBinding.tvConfirm).subscribe(o -> {
            Intent intent = new Intent();
            intent.putExtra("facilityName", mBinding.etName.getText().toString());
            intent.putExtra("identifierId", identifierId);
            intent.putExtra("mid", mid);
            //获得目标Fragment,并将数据通过onActivityResult放入到intent中进行传值
            getTargetFragment().onActivityResult(RECHRISTEN_CODDE, Activity.RESULT_OK, intent);
            dismiss();
        });
    }
}
